package com.wbdgj.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.internal.LinkedTreeMap;
import com.wbdgj.R;
import com.wbdgj.http.HttpAdapter;

import java.text.DecimalFormat;
import java.util.List;

/**
 * ListAdapter
 * Created by XiaoWei on 2015-11-14.
 */
public class WordResultAdapter extends BaseListAdapter<LinkedTreeMap> {

    public WordResultAdapter(Context context, List<LinkedTreeMap> objects) {
        super(context, objects);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            if (getItemViewType(position) == 0) {
                convertView = ((Activity) (mContext)).getLayoutInflater().inflate(R.layout.layout_has_no_data, parent, false);
                holder.noDataRootLayout = (LinearLayout) convertView.findViewById(R.id.root_layout);
            } else {
                convertView = ((Activity) (mContext)).getLayoutInflater().inflate(R.layout.ui_word_compaign_result_dialog_item, parent, false);
                holder.use_end = (TextView) convertView.findViewById(R.id.use_end);
                holder.amount = (TextView) convertView.findViewById(R.id.amount);
                holder.min_amount = (TextView) convertView.findViewById(R.id.min_amount);
                holder.count = (TextView) convertView.findViewById(R.id.count);
                holder.name = (TextView) convertView.findViewById(R.id.name);
                holder.swjImg = (ImageView) convertView.findViewById(R.id.swjImg);
                holder.swjLay = (LinearLayout) convertView.findViewById(R.id.swjLay);
                holder.type_name = (Button) convertView.findViewById(R.id.type_name);
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (hasNoData) {
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(getScreenWidth(), getScreenHeight() * 2 / 3);
            holder.noDataRootLayout.setLayoutParams(lp);
        } else {
            final DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
            holder.use_end.setText((mDataList.get(position).get("use_end")+""));
            holder.amount.setText(decimalFormat.format(mDataList.get(position).get("amount")));
            holder.min_amount.setText("消费满"+decimalFormat.format(mDataList.get(position).get("min_amount"))+"元可用");
            holder.count.setText("X"+decimalFormat.format(mDataList.get(position).get("count")));
            holder.name.setText((mDataList.get(position).get("name")+""));
            holder.type_name.setText((mDataList.get(position).get("type_name")+""));
            Glide.with(mContext).load(mDataList.get(position).get("prize_img")+"").placeholder(R.mipmap.swj_holder_bg).error(R.mipmap.swj_holder_bg).into(holder.swjImg);
            if(decimalFormat.format(mDataList.get(position).get("type")).equals("5")){
                holder.swjImg.setVisibility(View.VISIBLE);
                holder.swjLay.setVisibility(View.GONE);
            }else{
                holder.swjImg.setVisibility(View.GONE);
                holder.swjLay.setVisibility(View.VISIBLE);
            }
        }

        return convertView;
    }

    private static final class ViewHolder {
        TextView use_end;
        TextView amount;
        TextView min_amount;
        TextView count;
        TextView name;
        Button type_name;
        ImageView swjImg;
        LinearLayout swjLay;
        LinearLayout noDataRootLayout;
    }

    private int getScreenWidth() {
        DisplayMetrics displayMetric = Resources.getSystem().getDisplayMetrics();
        return displayMetric.widthPixels;
    }

    private int getScreenHeight() {
        DisplayMetrics displayMetric = Resources.getSystem().getDisplayMetrics();
        return displayMetric.heightPixels;
    }

}
